//
//  LogoSmash.m
//  PezMonkey
//
//  Created by Spence DiNicolantonio on 11/11/09.
//  Copyright 2009 Necrosoft. All rights reserved.
//

#import "LogoSmash.h"
#import "logoBitmap.h"


@implementation LogoSmash

/**
 * Initializes the receiver.
 */
- (void)start {
	self.space= [PMSpace space];
	[space setActiveHashSize:10000 cellDimension:2];
	[space setSubSteps:1];
	[space setIterations:1];
	
	PMBody *body;
	PMShape *shape;
	
	// create Logo from bitmap
	for(int y=0; y<imageHeight; y++) {
		for(int x=0; x<imageWidth; x++) {
			
			// no pixel?
			if (!myImageData[x + y*imageWidth])
				continue;
			
			// make ball on pixel
			PMVect point = PMVectMake(2 * (x - imageWidth/2), 
									  2 * (imageHeight/2 - y));
			shape = [self makeBallAtPoint:point];
			
			// add ball body and shape to space
			[space addBody:shape.body];
			[space addShape:shape];
		}
	}
	
	// create projectile body
	body = [PMBody staticBody];
	body.position = PMVectMake(-1000.0, 0.0);
	body.velocity = PMVectMake(400.0, 0.0);
	[space addBody:body];
	
	// create projectile shape
	shape = [PMCircleShape circleShapeWithBody:body radius:8.0];
	shape.elasticity = 0.0;
	shape.friction = 0.0;
	shape.layers = PMLayerMaskNotGrabbable;
	[space addShape:shape];
}

/**
 * Returns the demo options to be used for the receiver.
 */
- (PMDemoDrawSettings)drawSettings {
	PMDemoDrawSettings settings = PMDemoDrawSettingsDefault;
	settings.drawShapes = NO;
	settings.fillShapes = NO;
	settings.collisionPointSize = 1.8;
	settings.bodyPointSize = 2.3;
	return settings;
}

/**
 * Creates a circular ball used to construct the image.
 */
- (PMCircleShape *)makeBallAtPoint:(PMVect)point {
	// create rigid body and set position
	PMBody *body = [PMBody bodyWithMass:1.0 moment:PMInfinity];
	body.position = point;
	
	// create collision shape and set friction and elasticity
	PMCircleShape *shape = [PMCircleShape circleShapeWithBody:body radius:0.95];
	[shape setElasticity:0.0];
	[shape setFriction:0.0];
	
	return shape;
}

@end
